package com.example.stella.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.stella.R;
import com.example.stella.models.Transaction;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class TransactionAdapter extends RecyclerView.Adapter<TransactionAdapter.ViewHolder> {
    private Context context;
    private List<Transaction> transactions;
    private DecimalFormat currencyFormat;
    private SimpleDateFormat dateFormat;

    public TransactionAdapter(Context context) {
        this.context = context;
        this.transactions = new ArrayList<>();
        this.currencyFormat = new DecimalFormat("¥#,##0.00");
        this.dateFormat = new SimpleDateFormat("MM-dd", Locale.getDefault());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_transaction, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Transaction transaction = transactions.get(position);
        
        holder.tvCategory.setText(getCategoryIcon(transaction.getCategory()));
        holder.tvCategoryName.setText(transaction.getCategory());
        holder.tvDescription.setText(transaction.getDescription().isEmpty() ? "无备注" : transaction.getDescription());
        holder.tvDate.setText(dateFormat.format(transaction.getDate()));
        
        String amountText = (transaction.getType().equals("支出") ? "-" : "+") + currencyFormat.format(transaction.getAmount());
        holder.tvAmount.setText(amountText);
        
        int color = transaction.getType().equals("支出") ? 
            context.getResources().getColor(R.color.expense_color) :
            context.getResources().getColor(R.color.income_color);
        holder.tvAmount.setTextColor(color);
    }

    @Override
    public int getItemCount() {
        return transactions.size();
    }

    public void setTransactions(List<Transaction> transactions) {
        this.transactions = transactions;
        notifyDataSetChanged();
    }

    private String getCategoryIcon(String category) {
        switch (category) {
            case "工资": case "奖金": case "兼职": case "投资": case "其他收入": return "💰";
            case "餐饮": return "🍽️";
            case "交通": return "🚌";
            case "购物": return "🛒";
            case "娱乐": return "🎬";
            case "医疗": return "🏥";
            case "住房": return "🏠";
            case "教育": return "📚";
            case "其他支出": return "💸";
            default: return "💰";
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvCategory, tvCategoryName, tvDescription, tvDate, tvAmount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCategory = itemView.findViewById(R.id.tvCategory);
            tvCategoryName = itemView.findViewById(R.id.tvCategoryName);
            tvDescription = itemView.findViewById(R.id.tvDescription);
            tvDate = itemView.findViewById(R.id.tvDate);
            tvAmount = itemView.findViewById(R.id.tvAmount);
        }
    }
} 